package com.flute.framework.search.highlight;

import java.util.*;

import com.flute.framework.spliter.WordSpliter;
import com.flute.tools.util.StringUtil;
import com.flute.tools.word.ISpliter;

/**
 * 标红策略实现类
 * 
 * @author Dave
 * 
 */
public class SplitedMarkStrategy implements IMarkStrategy {

	private ISpliter spliter;

	public ISpliter getSpliter() {
		return spliter;
	}

	public void setSpliter(ISpliter spliter) {
		this.spliter = spliter;
	}

	/**
	 * @see com.flute.Flute.mark.IMarkStrategy#getMarked(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public MarkPosition[] getMarked(String keyword, String searchString) {
		List<MarkPosition> set = new ArrayList<MarkPosition>();
		String newss = StringUtil.getStandardString(searchString);
		if (spliter == null) {
			spliter = WordSpliter.getInstance();
		}
		String[] ks = spliter.split(StringUtil.getStandardString(keyword));
		for (String k : ks) {
			int[] idx = StringUtil.getAllInDependentIndex(newss, k);
			for (int id : idx) {
				MarkPosition mp = new MarkPosition();
				mp.setSource(searchString);
				mp.setStart(id);
				mp.setEnd(id + k.length());
				set.add(mp);
			}
		}
		return set.toArray(new MarkPosition[] {});
	}

}
